<?php
class tagsModel extends baseModel{
    protected $table = 'tags';

    //tags列表
    public function tags_list($limit=null,$where=null,$order=null) {

        $sql="
            SELECT A.*,B.name as cname
            FROM {$this->model->pre}tags A 
            LEFT JOIN {$this->model->pre}tags_category B ON A.cid = B.cid
            {$where} ORDER BY {$order}A.id DESC LIMIT {$limit}
            ";
        $data=$this->model->query($sql);
        return $data;
    }

    //tag总数
    public function count($where=null) {

        $sql="
            SELECT count(*) as num
            FROM {$this->model->pre}tags A 
            LEFT JOIN {$this->model->pre}tags_category B ON A.cid = B.cid
            {$where}
            ";
        $data=$this->model->query($sql);
        return $data[0]['num'];
    }

    //查询tag信息
    public function info($name) {
        $condition['name']=$name;
        return $this->find($condition);
    }

    //修改TAG
    public function edit($data)
    {
        return $this->update('id='.$data['id'],$data);
    }

    //删除tag内容
    public function del($id)
    {
        $this->delete('id='.$id); 
        model('tags_relation')->relation_del($id);
    }

    //关联tag
    public function content_save($keywords,$aid)
    {
        if(empty($keywords)){
            return false;
        }
        $str = $keywords;
        $str = str_replace('，', ',', $str);
        $str = str_replace(' ', ',', $str);
        $strArray = explode(",", $str);
        //删除关联
        $this->del_content($aid);
        foreach ($strArray as $name)
        {
            if(!empty($name)){
                $info=$this->info($name);
                if (empty($info))
                {
                    //添加tag
                    $data = array();
                    $data['name'] = $name;
                    $data['quote']=1;
                    $tid=$this->insert($data);
                    //添加关联
                    $data_relation['aid']=$aid;
                    $data_relation['tid']=$tid;
                    model('tags_relation')->add($data_relation);
                }
                else
                {
                    //查找关联
                    $condition=array();
                    $condition['aid']=$aid;
                    $condition['tid']=$info['id'];
                    $info_relation=model('tags_relation')->info($condition);

                    //关联信息
                    if(empty($info_relation)){
                        $data_relation=array();
                        $data_relation['aid']=$aid;
                        $data_relation['tid']=$info['id'];
                        model('tags_relation')->add($data_relation);
                    }

                    //增加引用次数
                    $this->table='tags';
                    $data = array();
                    $data['quote']=intval($info['quote'])+1;
                    $this->update('id='.$info['id'],$data);
                }

            }

        }
        return true;
    }

    //删除tag
    public function del_content($aid)
    {
        $list=model('tags_relation')->relation_list($aid);
        if(empty($list)){
            return;
        }
        //删除该内容的TAG关系
        model('tags_relation')->content_del($aid);
        //查找其他TAG关系
        foreach ($list as $value) {
            $info=model('tags_relation')->info('tid='.$value['tid']);
            if(empty($info)){
                $this->delete('id='.$value['tid']);
            }
        }
    }

    //内容列表
    public function content_list($tid,$limit) {
        if(empty($tid)){
        return;
        }
        $sql="
            SELECT A.*,B.name as cname,B.subname as csubname,B.mid
            FROM {$this->model->pre}content A 
            INNER JOIN {$this->model->pre}category B ON A.cid = B.cid
            INNER JOIN {$this->model->pre}tags_relation C ON C.aid = A.aid
            WHERE A.status=1 AND C.tid={$tid} ORDER BY A.updatetime DESC,A.aid DESC LIMIT {$limit}
            ";
        $data=$this->model->query($sql);
        return $data;
    }

    public function content_count($tid) {
        if(empty($tid)){
        return;
        }
        $sql="
            SELECT count(*) as num
            FROM {$this->model->pre}content A 
            INNER JOIN {$this->model->pre}category B ON A.cid = B.cid
            INNER JOIN {$this->model->pre}tags_relation C ON C.aid = A.aid
            WHERE A.status=1 AND C.tid={$tid}
            ";
        $data=$this->model->query($sql);
        return $data[0]['num'];
    }

    //替换TAG连接
    public function content_tag_link($info=array()){
        $content=$info['content'];
        if(empty($content)||!$info['taglink']){
            return $info;
        }
        $sql="
            SELECT A.*
             FROM {$this->model->pre}tags A 
             LEFT JOIN {$this->model->pre}tags_relation B ON B.tid=A.id
             WHERE B.aid={$info['aid']}
        ";
        $taglist = $this->model->query($sql);
        $content=html_out($content);
        if (!empty($taglist)) {
            foreach ($taglist as $export) {
                if(!empty($export['name'])){
                    $content = preg_replace("/(?!<[^>]+)".preg_quote($export['name'],'/')."(?![^<]*>)/", '<a href="'.url('tags/index/info',array('name'=>$export['name'])).'"  target="_blank">'.$export['name'].'</a>',$content,1);
                }
            }
        }
        $info['content']=$content;
        return $info;
    }

    


	
}